﻿using System;
using System.Linq;
using Stimulus360.Services;
using Stimulus360.Models.Common;
using System.Collections.Generic;
using Stimulus360.Services.Interfaces;
using System.Text.RegularExpressions;

namespace Stimulus360.Models.Data
{
    /// <summary>
    ///     Partial class for UserProfile entity to add custom functionality.
    /// </summary>
    public partial class UserProfile
    {
        /// <summary>
        ///     String representing the user's current/new password
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        ///     String representing the user's current password
        /// </summary>
        public string OldPassword { get; set; }

        /// <summary>
        ///     String representing the retyped password
        /// </summary>
        public string ConfirmPassword { get; set; }

        /// <summary>
        ///     Bool to indicate if Captcha was valid or not.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Captcha", Justification ="Captcha is a valid term")]
        public bool CaptchaValid { get; set; }

        /// <summary>
        ///     The Membership service to be used.
        /// </summary>
        public IMembershipService MembershipService
        {
            get;
            private set;
        }


        /// <summary>
        ///     Boolean indicating if there are any rule voilations in the User profile
        ///     definition
        /// </summary>
        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        /// <summary>
        ///     Boolean indicating if there are any rule voilations in the User
        ///     profile edit definition
        /// </summary>
        public bool EditIsValid
        {
            get { return (GetEditRuleViolations().Count() == 0); }
        }

        /// <summary>
        ///     Method to get the list of Rule Voilations for the current
        ///     user profile instance
        /// </summary>
        /// <returns>
        ///     List of RuleVoilations if any
        /// </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
            Justification = "Method use is appropriate considering complexity and logic http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx#cpconpropertyusageguidelinesanchor1")]
        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            bool passwordProvided = true;
            bool confirmPasswordProvided = true;

            MembershipService = new MembershipService();

            if (string.IsNullOrEmpty(FirstName))
                yield return new RuleViolation(Resources.Stimulus360.Rule_FirstName, "FirstName");

            if (string.IsNullOrEmpty(UserName))
                yield return new RuleViolation(Resources.Stimulus360.Rule_CommunityAliasReq, "UserName");

            if (string.IsNullOrEmpty(Email))
            {
                yield return new RuleViolation(Resources.Stimulus360.Rule_Email, "Email");
            }
            else if (!string.IsNullOrEmpty(Email))
            {
                if (!Regex.IsMatch(Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
                {
                    yield return new RuleViolation(Resources.Stimulus360.Rule_EmailId, "Email");
                }
            }
            if (string.IsNullOrEmpty(Password))
            {

                yield return new RuleViolation(Resources.Stimulus360.Rule_Password, "Password");
                passwordProvided = false;

            }
            else if (Password.Length < MembershipService.MinPasswordLength)
            {
                yield return
                    new RuleViolation(
                        string.Format(Resources.Stimulus360.Rule_PasswordChar, MembershipService.MinPasswordLength),
                        "Password");
                passwordProvided = false;
            }
            if (passwordProvided)
            {
                if (string.IsNullOrEmpty(ConfirmPassword))
                {
                    yield return new RuleViolation(Resources.Stimulus360.Rule_ConfirmPwd, "ConfirmPassword");
                    confirmPasswordProvided = false;
                }
            }
            if ((!string.IsNullOrEmpty(UserName)) && (!string.IsNullOrEmpty(Password)))
            {
                if (MembershipService.UserExists(UserName))
                {
                    yield return new RuleViolation(Resources.Stimulus360.DuplicateUserName , "UserName");
                }
            }

            if (passwordProvided && confirmPasswordProvided)
            {
                if (!String.Equals(Password, ConfirmPassword, StringComparison.Ordinal))
                    yield return new RuleViolation(Resources.Stimulus360.Rule_New_ConfmPwd, "Password");
            }

            if (!CaptchaValid)
                yield return new RuleViolation(Resources.Stimulus360.Rule_Captcha, "captchaValid");

        }


        /// <summary>
        ///     Method to get the list of Rule Voilations for the edit user profile
        ///     instance
        /// </summary>
        /// <returns>
        ///     List of RuleVoilations if any
        /// </returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate",
            Justification = "Method use is appropriate considering complexity and logic http://msdn.microsoft.com/en-us/library/bzwdh01d(VS.71).aspx#cpconpropertyusageguidelinesanchor1")]
        public IEnumerable<RuleViolation> GetEditRuleViolations()
        {
            MembershipService = new MembershipService();

            if (string.IsNullOrEmpty(FirstName))
                yield return new RuleViolation(Resources.Stimulus360.Rule_FirstName, "FirstName");

            if (string.IsNullOrEmpty(UserName))
                yield return new RuleViolation(Resources.Stimulus360.Rule_CommunityAliasReq, "UserName");

            if (string.IsNullOrEmpty(Email))
                yield return new RuleViolation(Resources.Stimulus360.Rule_Email, "Email");

            if (!string.IsNullOrEmpty(Email))
            {
                if (!Regex.IsMatch(Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
                {
                    yield return new RuleViolation(Resources.Stimulus360.Rule_EmailId, "Email");
                }
            }
            if (!(string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(OldPassword) && string.IsNullOrEmpty(ConfirmPassword)))
            {

                if (string.IsNullOrEmpty(Password))
                    yield return new RuleViolation(Resources.Stimulus360.Rule_NewPasswordReq, "Password");
                else if (Password.Length < MembershipService.MinPasswordLength)
                    yield return
                        new RuleViolation(
                            string.Format(Resources.Stimulus360.Rule_PasswordChar, MembershipService.MinPasswordLength),
                            "Password");


                if (string.IsNullOrEmpty(OldPassword))
                {
                    yield return new RuleViolation(Resources.Stimulus360.Rule_OldPwdReq, "OldPassword");
                }

                if (string.IsNullOrEmpty(ConfirmPassword))
                    yield return new RuleViolation(Resources.Stimulus360.Rule_ConfirmPwd, "ConfirmPassword");

                if ((!string.IsNullOrEmpty(Password)) && (!string.IsNullOrEmpty(ConfirmPassword)))
                {
                    if (!String.Equals(Password, ConfirmPassword, StringComparison.Ordinal))
                        yield return
                            new RuleViolation(Resources.Stimulus360.Rule_New_ConfmPwd, "ConfirmPassword");
                }

                if (!string.IsNullOrEmpty(OldPassword))
                {
                    if (!MembershipService.ValidateUser(UserName, OldPassword))
                    {
                        yield return new RuleViolation(Resources.Stimulus360.Rule_OldPwdMismatch, "OldPassword");
                    }
                }
            }
        }

    }
}
